﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Service.Core;
using Service.Core.Interface;
using Service.Core.Result;
using Tools;
using Web.BML.AnalysisData;
using Web.BML.CoreData;
using Web.Models.AnalysisData;
using Web.Models.CoreData;
using Web.ViewModel.CoreData;

namespace Web.Service
{
    public  class ReportService : BaseService
    {
        private ReportOp reportOp = ReportOp.GetInstance();

        private static ReportService instance;

        private ReportService()
        {

        }

        public static ReportService GetInstance()
        {
            if (instance == null)
            {
                instance = new ReportService();
            }
            return instance;
        }

        /// <summary>
        /// 根据用户许可号读取报文列表数据
        /// </summary>
        /// <returns></returns>
        public List<ReportModel> GetReportList(long startTime,long endTime,string account,String mmsi="",String type="")
        {
            var list = CacheService.GetReport(account).Where(m => m.TimeStamp > startTime && m.TimeStamp < endTime).ToList();
            if (!string.IsNullOrEmpty(mmsi))
            {
                list = list.Where(m => m.MMSI == mmsi).ToList();
            }
            if (!string.IsNullOrEmpty(type))
            {
                list = list.Where(m => m.Type == type).ToList();
            }
            return list.ToList();
            //return reportOp.QueryReportList(user.MMSI,start, end).Select(m => ReportModel.ConvertMondel(m)).ToList();
        }

        public List<ReportCountModel> CountReport(long startTime, long endTime, string account)
        {
            List<ReportModel> reportList = GetReportList(startTime,endTime,account);
            User user = UserOp.GetInstance().QueryUser(account);
            List<ReportCountModel> result = new List<ReportCountModel>();
            List<ShipInfo> shipInfos = ShipOp.GetInstance().QueryAllShips();
            foreach (String mmsi in user.MMSI.Split(','))
            {
                if(string.IsNullOrEmpty(mmsi))
                {
                    continue;
                }

                result.Add(new ReportCountModel()
                {
                    ShipName = ((ShipXYModel)JsonHelper.DeserializeObject(shipInfos.FirstOrDefault(m=>m.Value.Contains(mmsi))?.Value,typeof(ShipXYModel))).name,
                    MMSI = mmsi,
                    ReportList= reportList.Where(m => m.MMSI.Equals(mmsi)).ToList()
                });
            }
            return result;
        }

        public IServiceResult ModifReports()
        {
            List<Report> reports = reportOp.QueryAllReportList();
            return ModifReports(reports);
        }

        /// <summary>
        /// 更新报文数据
        /// </summary>
        /// <returns></returns>
        public IServiceResult ModifReports(List<Report> reports)
        {
            foreach (var report in reports)
            {
                ReportDetailModel model = (ReportDetailModel)JsonHelper.DeserializeObject(report.Value, typeof(ReportDetailModel));
                report.ShipName = model.ShipInfo.ShipName;
                report.ReportTime = DateTimeHelper.ConvertDateTimeInt(model.DataUin.SendTime);
                report.ReportTime = report.ReportTime == -2147483648 ? 0 : report.ReportTime;
            }
            reportOp.ModifReports(reports);
            return Result(ResultType.Success);
        }

        


    }
}